<?php
defined( "_SBKH_" ) or die( 'ERROR : RESTRICT ACCESS THIS LOCATION.' );
class Database{
	private static $_host = "";
	private static $_password = "";
	private static $_username = "";
	private static $_database = "";
	private static $_tb_prefix = "tbl_";
	private static $_conn = "";
	
	private function __construct(){
		self::$_host = "localhost";
		self::$_username = "root";
		self::$_database = "skh_ice_factory";
		self::$_password = "root";	
		self::$_tb_prefix = "tbl_";	
	}
	public function GetPrefix($table_name = ""){
		return "`" . self:: $_tb_prefix . $table_name . "`";
	}
	/*
	 ** function ConnectDB() to connect to Database
	*/
	public static function ConnectDB(){
		new Database();
		self::$_conn = mysql_connect(self::$_host,self::$_username,self::$_password) or die("ERROR: Connect Database Failed!!!");
		mysql_select_db(self::$_database) or die("ERROR: Database" . self::$_database . " Not Found!!!");
	}
	/*
	** Function Query() to execute SQL : Insert, Update, Delete, Select
	** Optional Parameter
	*/
	public function Query($sql = "",$show_query = true){
		$timezone = "Asia/Bangkok";
		if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
		if(!empty($sql)){
			$help = 0;
			$this-> connectDB();
			mysql_query('Set Names `utf8`');
			if(!$show_query) die($sql);
			
			$result = mysql_query($sql) or die("ERROR: " . mysql_error());
			if( mysql_insert_id() == 0 && @mysql_num_rows($result) != ""){
				$list = array();
				while($row = mysql_fetch_object($result)){
					$list[] = $row;
				}				
				$this->closeDB();
				return $list;
			}else{
				$t = array();
				if(mysql_insert_id() == 0){
					if(mysql_affected_rows() != 0){
						$t = mysql_affected_rows();
					}else $t = array();
				}else $t = mysql_insert_id();
				//return mysql_insert_id() == 0?mysql_affected_rows():mysql_insert_id();
				return $t;	
			}
		}
		return array();
	}
	public function Save($data = array(),$show_query = true){//pr($data);
		$sql = "INSERT INTO ";
		foreach($data as $table => $fields){
			$sql .= $this -> GetPrefix($table) . " SET ";
			$f = "";
			foreach($fields as $field_name => $field_value){				
				$f .= "`" . $field_name . "` = '" . escStr($field_value) . "',";										
			}
			if(!$show_query)
				die($sql . $f . " `created` = NOW(), `modified` = NOW()");
			return($this->query($sql . $f . " `created` = NOW(), `modified` = NOW()"));
		}		
	}
	public function Update($data = array(), $modified_field = true, $show_query = true){
		$sql = "UPDATE ";
		foreach($data as $table => $fields){
			$sql .= $this -> GetPrefix($table) . " SET ";
			if(isset($data[$table]['id'])){
				$f = "";
				foreach($fields as $field_name => $field_value){
					$f .= "`" . $field_name . "` = '" . escStr($field_value) . "',";
				}
				if($modified_field){
					if(!$show_query)
						die($sql . $f . " `modified` = NOW() WHERE `id` ='" . $data[$table]['id'] . "'");
					return($this->query($sql . $f . " `modified` = NOW() WHERE `id` ='" . $data[$table]['id'] . "'"));
				}else{
					if(!$show_query)
						die($sql . rtrim($f,",") . " WHERE `id` ='" . $data[$table]['id'] . "'");
					return($this->query($sql . rtrim($f,',') .  " WHERE `id` ='" . $data[$table]['id'] . "'" ));
				}
			}else{
				return 0;
			}
		}
	}
	/*
	 ** function closeDB() to disconnect from Database
	*/
	public function CloseDB(){
		mysql_close(self::$_conn);
	}
}
?>